<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never">
      <template #header>
        <div class="justify-between">
          <el-button
            v-if="permissions.create"
            v-hasPerm="[`sys:guarantee_income:add`]"
            type="primary"
            @click="handelCreate"
            >创建收入信息
          </el-button>
          <el-button
            v-if="permissions.export"
            v-hasPerm="[`sys:guarantee_income:export`]"
            v-loading="loadingExport"
            type="primary"
            @click="handelExport"
            >导出
          </el-button>
        </div>
      </template>
      <el-table v-loading="loading" :data="tableList" highlight-current-row border>
        <el-table-column label="项目编号" prop="projectNum" min-width="110" />
        <el-table-column label="项目名称" prop="projectName" min-width="110" />
        <el-table-column label="收入编号" prop="code" min-width="110" />
        <el-table-column label="交易类型" prop="tradingTypeName" min-width="100" />
        <el-table-column label="缴存类型" prop="depositeTypeName" min-width="100" />
        <el-table-column label="收入方式" prop="incomeWayName" min-width="100" />
        <el-table-column label="收入金额" prop="amount" min-width="100" />
        <el-table-column label="收入人" prop="creator" min-width="100" />
        <el-table-column label="收入时间" prop="payDate" min-width="100" />
        <el-table-column fixed="right" label="操作" align="center" width="80">
          <template #default="scope">
            <el-button
              v-if="permissions.delete"
              v-hasData="scope.row.hasEdit"
              v-hasPerm="[`sys:guarantee_income:delete`]"
              type="primary"
              link
              @click="handelDelete(scope.row)"
              >删除</el-button
            >
            <el-button v-if="permissions.detail" type="primary" link @click="handelDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageIndex"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
    <add-form :dialog-add-or-edit="dialogAddOrEdit" @send-close="handleSendClose"></add-form>
    <detail :dialog-detail="dialogDetail" @send-close="handleSendClose"></detail>
  </div>
</template>

<script lang="ts" setup>
import addForm from "./addForm.vue";
import detail from "./detail.vue";
import { downloadFile } from "@/utils";

import {
  QueryProjectGuaranteeIncome,
  DeleteProjectGuaranteeIncome,
  ExportProjectGuaranteeIncome,
} from "@/api/guarantee/index";
import { QueryPaymentForm } from "@/api/guarantee/types";

const permissions = ref({
  create: false,
  delete: false,
  detail: false,
  export: false,
});

const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor"));

if (currentSystem.value == "SysUser") {
  permissions.value.create = true;
  permissions.value.delete = true;
  permissions.value.detail = true;
  permissions.value.export = true;
}
if (currentSystem.value == "Manager") {
  permissions.value.export = true;
  permissions.value.detail = true;
}
if (currentSystem.value == "Constructor" || currentSystem.value == "Commander" || currentSystem.value == "Corp") {
}

const total = ref(0);
const loading = ref(false);
const loadingExport = ref(false);
const queryFormRef = ref(ElForm);
const tableList = ref<any[]>();
const queryParams = reactive<QueryPaymentForm>({
  pageIndex: 1,
  pageSize: 20,
  keywords: "",
});

const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
});
const dialogDetail = reactive<DialogOption>({
  visible: false,
});

//导出
const handelExport = () => {
  loadingExport.value = true;
  ExportProjectGuaranteeIncome(queryParams)
    .then((res) => {
      if (res.success) {
        downloadFile(res.data);
      } else {
      }
    })
    .finally(() => {
      loadingExport.value = false;
    });
};

//添加
const handelCreate = () => {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.title = "创建收入信息";
  dialogAddOrEdit.footBtn = "保存";
  dialogAddOrEdit.type = 1;
};

//详情
const handelDetail = (row: any) => {
  dialogDetail.visible = true;
  dialogDetail.id = row.id;
  dialogDetail.title = "收入信息详情";
};

//删除
const handelDelete = (row: any) => {
  ElMessageBox.confirm("确认删除吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      DeleteProjectGuaranteeIncome(row.id)
        .then((res) => {
          if (res.success === true) {
            handleQuery();
          }
        })
        .catch((err) => {
          console.log(err);
        });
    })
    .catch();
};

//关闭弹窗
const handleSendClose = function (needRefresh: boolean) {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  dialogDetail.visible = false;
  dialogDetail.id = undefined;
  if (needRefresh) {
    handleQuery();
  }
};

/* 列表查询 */
const handleQuery = async () => {
  loading.value = true;
  QueryProjectGuaranteeIncome(queryParams)
    .then((res) => {
      if (res.success) {
        tableList.value = res.data.list;
        queryParams.pageIndex = res.data.pageNum;
        queryParams.pageSize = res.data.pageSize;
        total.value = res.data.total;
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>

<style lang="less" scoped>
:deep(.detail .el-dialog__body) {
  height: var(--mobileAddFormHeight);
  overflow-y: auto;
}
</style>
